package admin_tag_api

import (
	"blog_go/global"
	"blog_go/models"
	"blog_go/models/res"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

type TagAddReq struct {
	Title string `json:"title" binding:"required" msg:"请输入标签title"` // 标签的名称
}

func (AdminTagApi) TagAdd(c *gin.Context) {
	var req TagAddReq
	if err := c.ShouldBindJSON(&req); err != nil {
		logrus.Error(err)
		panic(err)
	}
	// 查询title是否重复
	affected := global.DB.Where("title = ?", req.Title).First(&models.TagModel{}).RowsAffected
	if affected > 0 {
		panic("数据已经存在")
	}
	err := global.DB.Create(&models.TagModel{
		Title: req.Title,
	}).Error
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	res.Ok("添加成功", true, c)
}
